package codeformat;

import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.popup.ListPopup;
import com.intellij.openapi.util.NlsContexts;
import com.intellij.openapi.wm.StatusBar;
import com.intellij.openapi.wm.StatusBarWidget;
import com.intellij.openapi.wm.impl.status.EditorBasedWidget;
import com.intellij.util.Consumer;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.awt.event.MouseEvent;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class OffDutyStatusBarWidget extends EditorBasedWidget implements StatusBarWidget.MultipleTextValuesPresentation {
    private static final DateTimeFormatter TIME_FORMATTER = DateTimeFormatter.ofPattern("HH:mm:ss");
    private final OffDutyTimeConfig config;
    private ScheduledExecutorService executorService;
    private String currentDisplayText;
    
    public OffDutyStatusBarWidget(@NotNull Project project) {
        super(project);
        this.config = OffDutyTimeConfig.getInstance();
        initializeExecutorService();
    }

    @Override
    public void dispose() {
        if (executorService != null) {
            executorService.shutdown();
        }
    }

    @Override
    public @NonNls @NotNull String ID() {
        return "OffDutyTimer";
    }

    @Override
    public @NotNull WidgetPresentation getPresentation() {
        return this;
    }



    private void initializeExecutorService() {
        executorService = Executors.newSingleThreadScheduledExecutor();
        executorService.scheduleAtFixedRate(this::updateDisplay, 0, 1, TimeUnit.SECONDS);
    }

    private void updateDisplay() {
        LocalTime now = LocalTime.now();
        LocalTime offDutyTime = config.getOffDutyTime();
        
        // 计算剩余时间
        long secondsUntilOffDuty = offDutyTime.toSecondOfDay() - now.toSecondOfDay();
        
        // 如果已经过了下班时间，显示明天的倒计时
        if (secondsUntilOffDuty <= 0) {
            currentDisplayText="下班！还不跑干什么呢！";
        } else {
            // 转换为时分秒
            long hours = secondsUntilOffDuty / 3600;
            long minutes = (secondsUntilOffDuty % 3600) / 60;
            long seconds = secondsUntilOffDuty % 60;
            
            // 更新显示文本
            currentDisplayText = String.format("%d:%02d:%02d", hours, minutes, seconds);
        }
        
        // 更新UI
        if (myStatusBar != null) {
            myStatusBar.updateWidget(ID());
        }
    }

    @Override
    public void install(@NotNull StatusBar statusBar) {
        super.install(statusBar);
        updateDisplay();
    }

    @Override
    public @NotNull String getTooltipText() {
        return "距离下班还有: " + currentDisplayText;
    }

    @Override
    public @Nullable Consumer<MouseEvent> getClickConsumer() {
        return null;
    }

    @Override
    public @Nullable("null means the widget is unable to show the popup") ListPopup getPopupStep() {
        // 返回null表示不显示弹出菜单
        return null;
    }

    @Override
    public @Nullable String getSelectedValue() {
        return currentDisplayText;
    }

    public @Nullable @NlsContexts.StatusBarText String getText() {
        return currentDisplayText; // 返回要显示在状态栏的文本
    }
}